//
//  DemoPageVC.swift
//  BWUIKit_Example
//
//  Created by zhuxuhong on 2022/2/17.
//  Copyright © 2022 CocoaPods. All rights reserved.
//

import BWUIKit

class DemoPageVC: PageViewController {
    
    lazy var page1 = PageVC1()
    lazy var page2 = PageVC2()
    lazy var page3 = PageVC3()
    lazy var page4 = PageVC()
    
    lazy var pageVCs: [UIViewController] = [
        page1,
        page2,
        page3,
        page4,
    ]
    
    lazy var pageDatas: [String] = (1...3).map{
        "Page \($0)"
    }
    
    override func didLoad() {
        super.didLoad()
        
        setPages([
            pageAtIndex(0)!
        ])
        
        navigationItem.rightBarButtonItem = .init(barButtonSystemItem: .add, target: self, action: #selector(addPages))
        
        scrollView?.delegate = self
    }
    
    @objc func addPages(){
        pageDatas += (1...3).map{
            "Added Page: \(pageDatas.count + $0)"
        }
        reloadPages()
    }
    
    override func pageDidChange(page vc: UIViewController) {
        print("pageDidChange: \(pageIndex)")
        if pageIndex == pageDatas.count-1 {
            addPages()
        }
    }
}

//extension DemoPageVC: StaticPages {
//
//}

extension DemoPageVC: DynamicPages {
    func indexForPage(_ page: UIViewController) -> Int {
        if let vc = page as? PageVC,
           let idx = pageDatas.firstIndex(of: vc.data as! String) {
            return idx
        }
        return 0
    }
    
    var numberOfPages: Int {
        pageDatas.count
    }
    
    func pageAtIndex(_ index: Int) -> UIViewController? {
        return PageVC(data: pageDatas[index])
    }
    
    var autoBouncing: Bool {
        return true
    }
}

//extension DemoPageVC: CustomPages {
//    func pageBefore(page vc: UIViewController) -> UIViewController? {
//        if currentPageIndex > 0 {
//            currentPageIndex -= 1
//            return PageVC(data: pageDatas[currentPageIndex])
//        }
//        return nil
//    }
//    func pageAfter(page vc: UIViewController) -> UIViewController? {
//        if currentPageIndex < pageDatas.count-1 {
//            currentPageIndex += 1
//            return PageVC(data: pageDatas[currentPageIndex])
//        }
//        return nil
//    }
//}

class PageVC: ViewController {
    var data: Any!
    
    convenience init(data: Any) {
        self.init()
        
        self.data = data
    }
    override func didLoad() {
        super.didLoad()
        
        view.backgroundColor = .random
        let label = UILabel(frame: view.bounds)
        label.text = data as? String
        label.textAlignment = .center
        label.font = .systemFont(ofSize: 24)
        view.addSubview(label)
    }
    
    func reloadData() {
        view.backgroundColor = .random
    }
}

class PageVC3: ViewController {
    override func didLoad() {
        super.didLoad()
        
        print("didLoad 3")
        view.backgroundColor = .random
    }
}



class PageVC2: ViewController {
    override func didLoad() {
        super.didLoad()
        
        print("didLoad 2")
        view.backgroundColor = .random
    }
}

class PageVC1: ViewController {
    override func didLoad() {
        super.didLoad()
        
        print("didLoad 1")
        view.backgroundColor = .random
    }
}

extension UIColor  {
    //返回随机颜色
    class var random: UIColor {
        let red = CGFloat(arc4random()%256)/255.0
        let green = CGFloat(arc4random()%256)/255.0
        let blue = CGFloat(arc4random()%256)/255.0
        return .init(red: red, green: green, blue: blue, alpha: 1.0)
    }
}
